﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data;
using DevExpress.XtraEditors.Controls;

namespace SoufunData
{
    public partial class ZProjectDetailForm : DevExpress.XtraEditors.XtraForm
    {

        public ZProjectDetailForm()
        {
            InitializeComponent();
            
            _dtResult = new DataTable();
            _dtResult.Columns.Add("项目名称");
            _dtResult.Columns.Add("行政区");
            _dtResult.Columns.Add("占地面积");
            _dtResult.Columns.Add("总建筑面积");
              
            DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;
        }
         
        private DataTable _dtResult;
        private readonly SqlClass _sqlClass = new SqlClass();

        private void AdvertisingForm_Load(object sender, EventArgs e)
        {
           
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }

        
         
        private void LoatData()
        {
           
            StringBuilder strWhere = new StringBuilder();

            //strWhere.Append("PublicTime between '");
            //strWhere.Append(dateEdit_start.DateTime);
            //strWhere.Append("' and '");
            //strWhere.Append(dateEdit_end.DateTime);
            //strWhere.Append("' ");

            if (imgCombox_xzq.SelectedIndex != 0)
            {
                if (strWhere.ToString()!="")
                {
                    strWhere.Append(" and ");    
                }                
                strWhere.Append(" AdministratiVeregion='");
                strWhere.Append(imgCombox_xzq.Properties.Items[
                    imgCombox_xzq.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgCombox_loop.SelectedIndex != 0)
            {
                if (strWhere.ToString() != "")
                {
                    strWhere.Append(" and ");
                }
                strWhere.Append(" `Loop` ='");
                strWhere.Append(imgCombox_loop.Properties.Items[
                    imgCombox_loop.SelectedIndex].Description);
                strWhere.Append("' ");
            }

            if (textEdit_proName.Text.Trim() != "")
            {
                if (strWhere.ToString() != "")
                {
                    strWhere.Append(" and ");
                }
                strWhere.Append(radioGroup1.Properties.Items[
                    radioGroup1.SelectedIndex].Value);
                strWhere.Append(" like '%");
                strWhere.Append(textEdit_proName.Text);
                strWhere.Append("%'");
            }
            try
            {
                DataSet ds = _sqlClass.ZProjectDetailGetList(strWhere.ToString());
                if (ds == null || ds.Tables.Count == 0)
                {
                    XtraMessageBox.Show("数据库查询出错！请检查数据库连接是否正常！");
                    return;
                }
                GetResultData(ds.Tables[0]);

                BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl2.DataSource = ds.Tables[0];
                    gridView2.GroupPanelText = "共记"
                        + ds.Tables[0].Rows.Count + "条数据";
                }
                ));
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            } 
        }

        private void GetResultData(DataTable dt)
        {
            _dtResult.Clear();
            foreach (DataRow row in dt.Rows)
            {
                string proName = row["ProjectName"].ToString();
                string xzq = row["AdministratiVeregion"].ToString();
                float area = float.Parse(row["Area"].ToString());
                float totalArea = float.Parse(row["TotalArea"].ToString());

                DataRow[] drs = _dtResult.Select("项目名称='" 
                    + proName+"'");
                if (drs.Length==0)
                {
                    DataRow newRow = _dtResult.NewRow();
                    newRow["项目名称"] = proName;
                    newRow["行政区"] = xzq;
                    newRow["占地面积"] = area;
                    newRow["总建筑面积"] = totalArea; 
                    _dtResult.Rows.Add(newRow);
                }
                else
                {
                    drs[0]["占地面积"] = int.Parse(drs[0]["占地面积"].ToString()) + area;
                    drs[0]["总建筑面积"] = float.Parse(drs[0]["总建筑面积"].ToString()) +
                                      totalArea;
                }
            }
            BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl1.DataSource = _dtResult;
                    gridView1.GroupPanelText = "共计" + _dtResult.Rows.Count + "条数据";
                }));
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            gridView2.GroupPanelText = "正在查询...";
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }
         
        private void textEdit_proName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
               btn_select_Click(null,null);
            }
        }

      
        private void btn_toExcel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "项目详细数据";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView2.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }
 

        private void btn_importResultExcel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "项目详细数据统计";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }
    }
}